//
//  GKTabBar.swift
//  MicroBlogFunc
//
//  Created by GK on 16/4/8.
//  Copyright © 2016年 GK. All rights reserved.
//

import UIKit

class GKTabBar: UITabBar {

    override func layoutSubviews()
    {
        super.layoutSubviews()
        let itemCount = mainBarItemCount
        let itemW = self.bounds.size.width / CGFloat(itemCount)
        let newFrame = CGRect(x: 0, y: 0, width: itemW, height: self.bounds.size.height)
        var index = 0
        for subview in self.subviews
        {
            
            let classes: AnyClass = NSClassFromString("UITabBarButton")!
            if subview.isKindOfClass(classes)
            {
                subview.frame = CGRectOffset(newFrame, itemW * CGFloat(index), 0)
                index += 1
                if index == 2
                {
                    index += 1
                }
            }
            
        }
        self.composeBtn.frame = CGRect(x: 2 * itemW, y: 0, width: itemW, height: self.bounds.size.height)
    }
    
    //定义闭包
    var composeClosure: ( () -> () )?
    
    
    //点击事件
    @objc private func didClickComposeBtn()
    {   //调用闭包
        composeClosure?()
    }
    
    //懒加载加号按钮
    private lazy var composeBtn: UIButton = {
       
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        
        //添加点击事件
        btn.addTarget(self, action: #selector(self.didClickComposeBtn), forControlEvents:UIControlEvents.TouchUpInside)
        
        //添加到tabbar
        self.addSubview(btn)
        return btn
    }()
    
    
    
}
